package com.entrogy.controller;


import com.entrogy.bean.SchoolRoute;
import com.entrogy.service.SchoolRouteService;
import com.entrogy.utils.ApiReturnUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 校车线路控制层
 * @Author 许红松
 * @date 2019/5/10 0010 下午 2:29
 */
@RestController
@Transactional
@RequestMapping(value = "/schoolRoute")
@Api(description = "对校车线路操作的相关接口")
public class SchoolRouteController {
    @Autowired
    private SchoolRouteService schoolRouteService;//校车线路信息service接口

    /**
     *   校车线路管理分页
     * @return List
     */
    @ApiOperation(value = "分页获取校车线路管理信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "学校ID",name = "school_code",dataType = "int",paramType = "query",required = true),
            @ApiImplicitParam(value = "线路名称",name = "SchoolRouteName",dataType = "String",paramType = "query",required = false),
            @ApiImplicitParam(value = "当前页数",name="page",required = false,dataType = "int",paramType = "query"),
            @ApiImplicitParam(value = "每页条数",name = "total",dataType = "int",paramType = "query",required = false)
    })
    @RequestMapping(value = "/getSchoolRouteList",method = RequestMethod.POST)
    public List<Map<String,Object>> getSchoolRouteList( @RequestParam(value="school_code")Integer school_code, String SchoolRouteName ){
        Map<String,Object> map=new HashMap<>();
        map.put("school_code",school_code);
        if ( SchoolRouteName!=null && !"".equals(SchoolRouteName)){
            map.put("SchoolRouteName",SchoolRouteName);
        }
        List<Map<String,Object>> list=schoolRouteService.getSchoolRouteList(map);
        return list;
    }

    /**
     *   新增校车线路管理信息
     * @param school_code
     * @param schoolRoute
     * @return
     */
    @ApiOperation(value = "新增校车线路管理")
    @RequestMapping(value = "/addSchoolRoute",method = RequestMethod.POST)
    public Map<String,Object> addSchoolRoute(@ModelAttribute SchoolRoute schoolRoute, @RequestParam(value="school_code")Integer school_code){
        schoolRoute.setSchool_code(school_code);
        int  code=schoolRouteService.addSchoolRoute(schoolRoute);
        if (code==1){
            return ApiReturnUtil.success("新增成功","");
        }else{
            return ApiReturnUtil.success("新增失败","");
        }


    }

    /**
     *   修改校车线路管理信息
     * @return
     */
    @ApiOperation(value = "修改校车线路管理")
    @RequestMapping(value = "/updateSchoolRoute",method = RequestMethod.POST)
    public Map<String,Object> updateSchoolRoute(@ModelAttribute SchoolRoute schoolRoute,@RequestParam(value="school_code")Integer school_code){
        Map<String,Object> map=new HashMap<>();
        if (schoolRoute.getSchoolRouteName()!=null && !"".equals(schoolRoute.getSchoolRouteName())){
            map.put("SchoolRouteName",schoolRoute.getSchoolRouteName());
        }
        if (schoolRoute.getContent()!=null && !"".equals(schoolRoute.getContent())){
            map.put("content",schoolRoute.getContent());
        }
        if (schoolRoute.getRouteid()!=0 && !"".equals(schoolRoute.getRouteid())){
            map.put("Routeid",schoolRoute.getRouteid());
        }
        map.put("school_code",school_code);
        int  code=schoolRouteService.updateSchoolRoute(map);
        if (code==1){
            return ApiReturnUtil.success("修改成功","");
        }else{
            return ApiReturnUtil.success("修改失败","");
        }


    }


    /**
     *   删除校车线路管理信息
     * @return
     */
    @ApiOperation(value = "删除校车线路管理信息")
    @RequestMapping(value = "/deleteSchoolRoute",method = RequestMethod.POST)
    public Map<String,Object> deleteSchoolRoute(@RequestParam("school_code")Integer school_code,@RequestParam("Routeid") int  Routeid){
        if (school_code==null|| school_code==0){
            return ApiReturnUtil.error("您无操作权限！");
        }
        int  code= schoolRouteService.deleteSchoolRoute(Routeid);
        if(code==1){
            return ApiReturnUtil.success("删除成功","");
        }
        return ApiReturnUtil.error("删除失败");
    }
}
